package com.wenjie.original.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wenjie.original.entity.Order;
import com.wenjie.original.entity.OrderBox;
import com.wenjie.original.entity.vo.Order.OrderUpOrAddVo;
import com.wenjie.original.flex_config.baseInterface.Cus_ServiceImp;
import com.wenjie.original.mapper.OrderMapper;
import com.wenjie.original.service.IOrderBoxService;
import com.wenjie.original.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单信息  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-24 13:59:04
 */
@Service
public class OrderServiceImpl extends Cus_ServiceImp<OrderMapper, Order> implements IOrderService {

    @Autowired
    private IOrderBoxService orderBoxsService;


    @Override
    @Transactional
    public void cusSave(OrderUpOrAddVo.AddVo addVo) {
        List<OrderBox> boxs = new ArrayList<>();
        addVo.setOrderId(IdUtil.getSnowflakeNextId());
        this.save(addVo);
        JSONArray orderBoxs = addVo.getOrderBoxs();
        for (int i = 0; i < orderBoxs.size(); i++) {
            JSONObject obj = JSONUtil.parseObj(orderBoxs.get(i));
            OrderBox orderBox1 = new OrderBox();
            orderBox1.setOrderId(addVo.getOrderId());
            orderBox1.setBoxId(obj.getLong("boxId"));
            orderBox1.setOrderBoxByBoxNum(obj.getInt("orderBoxByBoxNum"));
            orderBox1.setOrderBoxByBoxLowWeight(obj.getStr("orderBoxByBoxLowWeight"));
            orderBox1.setOrderBoxByBoxHighWeight(obj.getStr("orderBoxByBoxHighWeight"));

            orderBox1.setOrderBoxBySequence(i + 1);
            boxs.add(orderBox1);
        }
        orderBoxsService.saveBatch(boxs);
    }
}
